
package poker.plans.player;

import java.util.Iterator;
import java.util.List;

import jadex.runtime.*;
import poker.beliefs.*;
import jadex.adapter.fipa.*;


public class ShowHandPlan extends Plan {

	public void body() {
	System.out.println("PLAYER "+getAgentName()+": I'm showing my hand");
	IMessageEvent request = (IMessageEvent) getInitialEvent();

	//We get the beliefs we need to create the tellHand message
	IMessageEvent answer = request.createReply("tellHand");
	Hand hand = (Hand) getBeliefbase().getBelief("hand").getFact();
	for(Card aux : hand.getHand()){
		System.out.println("Card "+aux.toString());
	}
	answer.setContent(hand);
	
	// Send the message
	sendMessage(answer);
	
	// IS IT NECESSARY TO TELL EVERYONE ABOUT MY HAND? MOREOVER, THERE'S NO PLAN
	// IMPLEMENTED TO RECEIVE THE HAND FROM ANOTHER PLAYER
	// IN THE SPECIFICATION THIS INTERACTION IS ONLY BETWEEN CROUPIER AND PLAYER
	IMessageEvent tellhand = this.createMessageEvent("tellHand");
	tellhand.setContent(hand);
	List<AgentIdentifier> players = (List) getBeliefbase().getBelief("players").getFact();
	// Add all players to the message
	AgentIdentifier id;
	Iterator it = players.iterator();
	while(it.hasNext()) {
		id = (AgentIdentifier) it.next();
		tellhand.getParameterSet(SFipa.RECEIVERS).addValue(id);
	}
	sendMessage(tellhand);
	}

}
